#region Copyright (c) 2006-2012 nHydrate.org, All Rights Reserved
//--------------------------------------------------------------------- *
//                          NHYDRATE.ORG                                *
//             Copyright (c) 2006-2012 All Rights reserved              *
//                                                                      *
//                                                                      *
//This file and its contents are protected by United States and         *
//International copyright laws.  Unauthorized reproduction and/or       *
//distribution of all or any portion of the code contained herein       *
//is strictly prohibited and will result in severe civil and criminal   *
//penalties.  Any violations of this copyright will be prosecuted       *
//to the fullest extent possible under law.                             *
//                                                                      *
//THE SOURCE CODE CONTAINED HEREIN AND IN RELATED FILES IS PROVIDED     *
//TO THE REGISTERED DEVELOPER FOR THE PURPOSES OF EDUCATION AND         *
//TROUBLESHOOTING. UNDER NO CIRCUMSTANCES MAY ANY PORTION OF THE SOURCE *
//CODE BE DISTRIBUTED, DISCLOSED OR OTHERWISE MADE AVAILABLE TO ANY     *
//THIRD PARTY WITHOUT THE EXPRESS WRITTEN CONSENT OF THE NHYDRATE GROUP *
//                                                                      *
//UNDER NO CIRCUMSTANCES MAY THE SOURCE CODE BE USED IN WHOLE OR IN     *
//PART, AS THE BASIS FOR CREATING A PRODUCT THAT PROVIDES THE SAME, OR  *
//SUBSTANTIALLY THE SAME, FUNCTIONALITY AS THIS PRODUCT                 *
//                                                                      *
//THE REGISTERED DEVELOPER ACKNOWLEDGES THAT THIS SOURCE CODE           *
//CONTAINS VALUABLE AND PROPRIETARY TRADE SECRETS OF NHYDRATE,          *
//THE REGISTERED DEVELOPER AGREES TO EXPEND EVERY EFFORT TO             *
//INSURE ITS CONFIDENTIALITY.                                           *
//                                                                      *
//THE END USER LICENSE AGREEMENT (EULA) ACCOMPANYING THE PRODUCT        *
//PERMITS THE REGISTERED DEVELOPER TO REDISTRIBUTE THE PRODUCT IN       *
//EXECUTABLE FORM ONLY IN SUPPORT OF APPLICATIONS WRITTEN USING         *
//THE PRODUCT.  IT DOES NOT PROVIDE ANY RIGHTS REGARDING THE            *
//SOURCE CODE CONTAINED HEREIN.                                         *
//                                                                      *
//THIS COPYRIGHT NOTICE MAY NOT BE REMOVED FROM THIS FILE.              *
//--------------------------------------------------------------------- *
#endregion

using System;
using System.ComponentModel;
using System.Xml;
using nHydrate.Generator.Common.GeneratorFramework;
using nHydrate.Generator.Common.Util;

namespace nHydrate.Generator.Models
{
	public enum ReferenceType
	{
		Column = 0,
		Table = 1,
		Relation = 2,
		CustomViewColumn = 3,
		CustomRetrieveRule = 4,
		Parameter = 5,
		CustomStoredProcedureColumn = 6,
		CustomAggregateColumn = 7,
		CustomView = 8,
		FunctionColumn = 9,
		FunctionParameter = 10,
		ViewRelation = 11,
	}

	public class Reference : BaseModelObject
	{
		#region Member Variables

		protected int _ref = 1;
		protected ReferenceType _refType = ReferenceType.Table;

		#endregion

		#region Constructor

		public Reference(INHydrateModelObject root)
			: base(root)
		{
		}

		#endregion

		#region Property Implementations

		[Browsable(false)]
		public int Ref
		{
			get { return _ref; }
			set { _ref = value; }
		}

		[Browsable(false)]
		public ReferenceType RefType
		{
			get { return _refType; }
			set { _refType = value; }
		}

		[Browsable(false)]
		public INHydrateModelObject Object
		{
			get
			{
				try
				{
					var modelRoot = (ModelRoot)this.Root;
					INHydrateModelObject retVal = null;
					switch(this.RefType)
					{
						case ReferenceType.Column:
							retVal = modelRoot.Database.Columns[Ref];
							break;
						case ReferenceType.Relation:
							retVal = modelRoot.Database.Relations.GetById(Ref);
							break;
						case ReferenceType.ViewRelation:
							retVal = modelRoot.Database.ViewRelations.GetById(Ref);
							break;
						case ReferenceType.Table:
							retVal = modelRoot.Database.Tables[Ref];
							break;
						case ReferenceType.CustomView:
							retVal = modelRoot.Database.CustomViews[Ref];
							break;
						case ReferenceType.CustomViewColumn:
							retVal = modelRoot.Database.CustomViewColumns[Ref];
							break;
						case ReferenceType.CustomRetrieveRule:
							retVal = modelRoot.Database.CustomRetrieveRules[Ref];
							break;
						case ReferenceType.Parameter:
							retVal = modelRoot.Database.CustomRetrieveRuleParameters[Ref];
							break;
						case ReferenceType.CustomStoredProcedureColumn:
							retVal = modelRoot.Database.CustomStoredProcedureColumns[Ref];
							break;
						case ReferenceType.FunctionColumn:
							retVal = modelRoot.Database.FunctionColumns[Ref];
							break;
						case ReferenceType.FunctionParameter:
							retVal = modelRoot.Database.FunctionParameters[Ref];
							break;
						default:
							throw new Exception("Cannot Handle Reference Type");
					}
					return retVal;
				}
				catch(Exception ex)
				{
					throw;
				}
			}
		}

		#endregion

		#region IXMLable Members

		public override void XmlAppend(XmlNode node)
		{
			try
			{
				var oDoc = node.OwnerDocument;

				XmlHelper.AddAttribute(node, "key", this.Key);
				XmlHelper.AddAttribute(node, "ref", this.Ref);
				XmlHelper.AddAttribute(node, "refType", (int)this.RefType);
			}
			catch(Exception ex)
			{
				throw;
			}
		}

		public override void XmlLoad(XmlNode node)
		{
			try
			{
				_key = XmlHelper.GetAttributeValue(node, "key", string.Empty);
				_ref = XmlHelper.GetAttributeValue(node, "ref", _ref);

				var refTypeNode = XmlHelper.GetAttributeValue(node, "refType", -1);
				if (refTypeNode != -1)
					_refType = (ReferenceType)refTypeNode;

				this.Dirty = false;
			}
			catch(Exception ex)
			{
				throw;
			}
		}

		#endregion

		#region Helpers

		public override string ToString()
		{
			var modelRoot = (ModelRoot)this.Root;
			var retval = base.ToString();
			switch(this.RefType)
			{
				case ReferenceType.Column:
					retval = modelRoot.Database.Columns[Ref].ToString();
					break;
				case ReferenceType.Relation:
					retval = modelRoot.Database.Relations.GetById(Ref).ToString();
					break;
				case ReferenceType.Table:
					retval = modelRoot.Database.Tables[Ref].ToString();
					break;
				case ReferenceType.CustomView:
					retval = modelRoot.Database.CustomViews[Ref].ToString();
					break;
				case ReferenceType.CustomViewColumn:
					retval = modelRoot.Database.CustomViewColumns[Ref].ToString();
					break;
				case ReferenceType.CustomRetrieveRule:
					retval = modelRoot.Database.CustomRetrieveRules[Ref].ToString();
					break;
				case ReferenceType.Parameter:
					retval = modelRoot.Database.CustomRetrieveRuleParameters[Ref].ToString();
					break;
				case ReferenceType.FunctionColumn:
					retval = modelRoot.Database.FunctionColumns[Ref].ToString();
					break;
				case ReferenceType.FunctionParameter:
					retval = modelRoot.Database.FunctionParameters[Ref].ToString();
					break;
			}
			return retval;
		}

		#endregion

	}

}